package com.onejson.ojmall.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.onejson.ojmall.common.domain.PageResult;
import com.onejson.ojmall.common.domain.ResponseDTO;
import com.onejson.ojmall.common.util.OjBeanUtil;
import com.onejson.ojmall.common.util.OjPageUtil;
import com.onejson.ojmall.dao.SMenuDao;
import com.onejson.ojmall.dao.SRoleDao;
import com.onejson.ojmall.dao.SRoleMenuDao;
import com.onejson.ojmall.model.entity.SMenuEntity;
import com.onejson.ojmall.model.entity.SRoleMenuEntity;
import com.onejson.ojmall.model.form.SRoleMenuBatchForm;
import com.onejson.ojmall.model.form.SRoleMenuForm;
import com.onejson.ojmall.model.form.SRoleMenuQueryForm;
import com.onejson.ojmall.model.vo.SMenuVO;
import com.onejson.ojmall.model.vo.SRoleMenuVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 角色-菜单 Service 层
 *
 * @author wxx
 */
@Service
@Slf4j
public class SRoleMenuService extends ServiceImpl<SRoleMenuDao, SRoleMenuEntity> {

    @Resource
    private SRoleMenuDao sRoleMenuDao;

    @Resource
    private SRoleDao roleDao;
    @Resource
    private SRoleMenuDao roleMenuDao;
//    @Resource
//    private SRoleMenuManager roleMenuManager;
    @Resource
    private SMenuDao menuDao;

    /**
     * 获取角色-菜单列表
     */
    public PageResult<SRoleMenuVO> getList(SRoleMenuQueryForm queryForm) {
        Page<?> page = OjPageUtil.convert2PageQuery(queryForm);
        List<SRoleMenuVO> list = sRoleMenuDao.query(page, queryForm);
        return OjPageUtil.convert2PageResult(page, list);
    }

    /**
     * 查询角色-菜单
     */
    public SRoleMenuVO queryById(String detailId) {

        SRoleMenuEntity detailEntity = sRoleMenuDao.selectById(detailId);
        SRoleMenuVO sRoleMenuVO = new SRoleMenuVO();

        try {
            BeanUtils.copyProperties(sRoleMenuVO, detailEntity);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sRoleMenuVO;

    }


    /**
     * 【角色-菜单】 - 添加
     */
    public ResponseDTO<String> addSRoleMenu(SRoleMenuForm addForm) {

        SRoleMenuEntity entity = new SRoleMenuEntity();

//        addForm.setId(null);

        try {
            BeanUtils.copyProperties(entity, addForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        sRoleMenuDao.insert(entity);
        return ResponseDTO.ok();
    }

    /**
     * 【角色-菜单】 - 更新
     */
    public ResponseDTO<String> updateSRoleMenu(SRoleMenuForm updateForm) {

        SRoleMenuEntity entity = new SRoleMenuEntity();

        try {
            BeanUtils.copyProperties(entity, updateForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        sRoleMenuDao.updateById(entity);
        return ResponseDTO.ok();
    }


    /**
     * 【角色-菜单】- 批量更新
     */
    public ResponseDTO<String> batchUpdateSRoleMenu(SRoleMenuBatchForm sRoleMenuBatchForm) {

        SRoleMenuEntity entity = new SRoleMenuEntity();
        List<SRoleMenuEntity> sRoleMenuEntityList = new ArrayList<>();

        try {
            for (SRoleMenuForm sRoleMenuForm : sRoleMenuBatchForm.getSRoleMenuFormList()) {

                sRoleMenuForm.setCreateTime(null);
                sRoleMenuForm.setUpdateTime(null);
                BeanUtils.copyProperties(entity, sRoleMenuForm);
                sRoleMenuEntityList.add(entity);
            }
        } catch (Exception e) {
            System.out.println("错误消息");
            System.out.println(e.getMessage());
        }
        this.updateBatchById(sRoleMenuEntityList);
        return ResponseDTO.ok();
    }


    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        sRoleMenuDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(String id) {
        if (null == id) {
            return ResponseDTO.ok();
        }

        sRoleMenuDao.deleteById(id);
        return ResponseDTO.ok();
    }



    /**
     * 根据角色id集合，查询其所有的菜单权限
     *
     */
    public List<SMenuVO> getMenuList(List<Long> roleIdList, Boolean administratorFlag) {
        //管理员返回所有菜单
        if(administratorFlag){
            List<SMenuEntity> menuEntityList = roleMenuDao.selectMenuListByRoleIdList(Lists.newArrayList(), false);
            return OjBeanUtil.copyList(menuEntityList, SMenuVO.class);
        }
        //非管理员 无角色 返回空菜单
        if (CollectionUtils.isEmpty(roleIdList)) {
            return new ArrayList<>();
        }
        List<SMenuEntity> menuEntityList = roleMenuDao.selectMenuListByRoleIdList(roleIdList, false);
        return OjBeanUtil.copyList(menuEntityList, SMenuVO.class);
    }
}

